package com.project.common.fundamental.repository;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.project.common.caching.CacheOption;

import com.project.common.caching.HashCacheRepository;
import com.project.common.caching.provider.CacheProvider;
import com.project.common.data.Persistence;
import com.project.common.data.mybatis.mapper.PersistenceMapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Component
public abstract class MapperRepository<K extends Serializable, P extends Persistence<K>,
        Mapper extends PersistenceMapper<K, P>> extends HashCacheRepository<K, P>
        implements PersistenceRepository<K, P> {
    private static final CacheOption DEFAULT_CACHE_OPTION = CacheOption.disable();

    @Value("${spring.datasource.driver-class-name:mysql}")
    private String driverClass;
    @Value("${cache.enabled}")
    private boolean cacheEnabled;

    public void setDriverClass(String driverClass) {
        this.driverClass = driverClass;
    }

    protected MapperRepository(CacheProvider cacheProvider, ObjectMapper objectMapper) {
        super(cacheProvider, objectMapper);
    }

    @Override
    public K addPersistence(P persistence) {
        // 将持久化对象插入数据库
        getMapper().insert(persistence);
        // 设置缓存信息
        if (cacheEnabled) {
            set(persistence);
        }
        return persistence.getKey();
    }

    @Override
    public void updatePersistence(P persistence) {
        Mapper mapper = this.getMapper();
        mapper.updateSelective(persistence);
        P updated = mapper.get(persistence.getKey());
        // 更新缓存
        if (cacheEnabled) {
            set(updated);
        }
    }

    @Override
    public void deletePersistence(K key) {
        // 删除缓存
        if (cacheEnabled) {
            delete(key);
        }
        getMapper().delete(key);
    }

    @Override
    public void deleteLogical(K key) {
        // 删除缓存
        if (cacheEnabled) {
            delete(key);
        }
        getMapper().deleteLogical(key);
    }

    @Override
    public P getPersistence(K key) {
        P value = null;
        // 先获取缓存
        if (cacheEnabled) {
            value = get(key);
        }
        if (value == null) {
            value = getMapper().get(key);
            if (value != null) {
                set(value);
            }
        }
        return value;
    }

    @Override
    public List<P> listPersistence() {
        return getMapper().listAll();
    }

    @Override
    protected CacheOption getCacheOption() {
        return DEFAULT_CACHE_OPTION;
    }

    protected abstract Mapper getMapper();

    @Override
    protected Boolean camelToLowerCase() {
        return false;
    }


    @Override
    public void addBatch(Collection<P> persistences) {
        // 将持久化对象插入数据库
        getMapper().insertBatch(persistences);
        // 设置缓存信息
        if (cacheEnabled) {
            set(persistences);
        }

    }

    @Override
    public void addOrUpdate(P persistence) {
        // 将持久化对象插入数据库
        getMapper().insertOrUpdate(persistence);
        // 设置缓存信息
        if (cacheEnabled) {
            set(persistence);
        }
    }

    @Override
    public void addOrUpdateBatch(Collection<P> persistences) {
        // 将持久化对象插入数据库
        getMapper().insertOrUpdateBatch(persistences);
        // 设置缓存信息
        if (cacheEnabled) {
            set(persistences);
        }
    }
}
